<?php
 class missing_admin extends controller{
   
      function missing_admin()
      {
        parent::Controller();
        $this->load->database();
      }

      function menu()
      {
        $dispform ='';
        $dispform .= '<a href="'.base_url().'missing_admin/support">Support</a>';
        
        echo $dispform;
      }   
      
      function support()
      {
           $displayform = '';
           
           
           
           $support_data = $this->db->query('select * from tbl_support_missing');
           $result = $support_data->result_array();
           
           $displayform .= '<div id=menu>';
           $displayform .= '<span style="float:right;"><input type="button" id="add" value="Add" /></span>';
           
          
           
           //print_r($result);
           foreach($result as $support)
           {
             
                $displayform .= '<span id='.$support['support_id'].' style="float:left;cursor:pointer;color:blue;">'.$support['support_heading'].'</span><br/>';
                $this->template->add_js('
                      $(document).ready(function(){
                        
                        var '.$support['support_heading'].' = '.$support['support_id'].';
                        $("#'.$support['support_id'].'").click(function(){
                                
                               $.post("/missing_admin/show_support_data", { "id": "'.$support['support_id'].'" },
                                      function(data){
                                                     $("#support_edit").html("<br/>"+data);                                                        
                                              }, "json");
                                
                        });
                      });
                
                ','embed');
           }
           
           $displayform .='</div>';
           
           //to edit support data
           $displayform .= '<div id=support_edit>';
           $displayform .= '</div>';
           $data = array('formdisplay'=>$displayform );
        
           $this->template->write_view('header','header', $data, True);
          $this->template->write_view('content', 'other_general');
          $this->template->render();
            
      }

      function show_support_data()
      {
            $data = $this->db->query("select * from tbl_support_missing where support_id=".$_POST['id']);
            $values = $data->result_array();
            
            $displayajax = '';
            
           
            
            $displayajax .= '<fieldset style="width:400px;">';
            $displayajax .= '<legend> Manage '.$values[0]['support_heading'].'</legend>';
            
            $sup_data = explode(',',$values[0]['support_data']);
                
            foreach($sup_data as $val)
            {
            	
                $displayajax .= '<div style="text-align:left;"><input type=radio name="sup_data" value="'.$val.'"><label>'.$val.'</label><br/></div>';
            }
            $displayajax .= '<input type="button" id="delete" value="Delete" />';
           
            
            $displayajax .= '<script>';
            $displayajax .= '$(document).ready(function(){
                        
                         
                         
                        $("#delete").click(function(){
                          
                          var id = '.$_POST['id'].',
                          sup_data = $("input:radio[name=sup_data]:checked").val();
                          
                                if(sup_data ==undefined)
                                {
                                  alert("Please select field to delete");
                                   return false;  
                                }
                                else
                                {
                                   $.post("/missing_admin/delete_data", { "id": id,"data":sup_data },
                                      function(){
                                               process();     
                                                                                                       
                                              }, "json");
                             
                                    function process()
                              {
                                
                                 $.post("/missing_admin/show_support_data", { "id": id },
                                      function(data){
                                                     $("#support_edit").html("<br/>"+data);                                                        
                                              }, "json");
                              }  
                              
                                }  
                           });
                           
                           
              
              $("#add").click(function(){
                        var id = '.$_POST['id'].',
                         sup_data = $("input:radio[name=sup_data]:checked").val();
                         
                            
                
                           });
              
              $("#edit").click(function(){
                         var id = '.$_POST['id'].',
                         sup_data = $("input:radio[name=sup_data]:checked").val();
                            
                                 if(sup_data ==undefined)
                                {
                                  alert("Please select field to delete");
                                   return false;  
                                }
                                else
                                {
                                   
                                }
                         
                         });
              
              
            });
            ';
            $displayajax .= '</script>';
            $displayajax .= '</fieldset>';
            echo json_encode($displayajax);
      }

      function delete_data()
      {
      	   print_r($_POST);exit;
           $data = $this->db->query("select support_data from tbl_support_missing where support_id=".$_POST['id']);
           $result = $data->result_array();
           
           $arr = '';
           $val = explode(',',$result[0]['support_data']);
           foreach( $val as $row)
           {
            
               if($row !=$_POST['data'])
               {
                 $arr .= $row;
                 $arr .=',';
               }
              
           }
     
           $final = substr($arr,0,-1);
          // echo $final;
           $query="update tbl_support_missing set support_data='".$final."' where support_id='".$_POST['id']."'";    
          // echo $query;
           $this->db->query($query);
      }
  
     

 }

?>
